package com.lyouu.dataminer.cruisetour;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.lyouu.dataminer.db.DbConstants;
import com.lyouu.dataminer.db.DbLyouuParams;
import com.lyouu.dataminer.factory.DaoFactory;
import com.lyouu.dataminer.factory.MysqlDbFactory;
import com.lyouu.dataminer.util.Constants;
import com.lyouu.dataminer.util.TourMap;
import com.lyouu.dataminer.util.Util;

public class CTPandaDe {
	// private static final String CURRENT_YEAR = String.valueOf(Calendar
	// .getInstance().get(Calendar.YEAR));
	private Logger logger = Logger.getLogger(CTDehuaDe.class);
	private Connection conn;
	private String currency;
	private String[] briefs;
	private String[] groups;

	public CTPandaDe() {

	}

	public void getData() throws SQLException {
		conn = MysqlDbFactory.getConnection(DbConstants.DB_LYOUU_SRC);
		String sql = "SELECT * FROM ct_panda_de";
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		while (rs.next()) {
			briefs = rs.getString(DbConstants.LYOUU_SRC_BUS_TOUR_BRIEF).split(
					Constants.DELIMITER);
			groups = rs.getString(DbConstants.LYOUU_SRC_BUS_TOUR_TRAVEL_GROUP)
					.split(Constants.DELIMITER);
		}
	}

	public void printData() {
		OutputStream os = null;
		try {
			os = new FileOutputStream("Z:\\Downloads\\ct_panda_de.txt");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		final PrintStream ps = new PrintStream(os);
		for (int i = 0; i < briefs.length; i++) {
			ps.println(briefs[i]);
			ps.println(groups[i]);
			ps.println();
		}
	}

	public void updateDbLyouu() throws SQLException {
		String name, content, departCity, routine, departCountry;
		// bustour_schedule table
		ArrayList<String> pageUrlList = new ArrayList();
		ArrayList<String> btsGroupIdList = new ArrayList<String>();
		ArrayList<String> btsDepartDateList = new ArrayList<String>();
		ArrayList<String> btsEndDateList = new ArrayList<String>();
		ArrayList<String> btsAdultPriceList = new ArrayList<String>();
		ArrayList<String> btsChildPriceList = new ArrayList<String>();
		ArrayList<String> btsGroupPriceList = new ArrayList<String>();
		ArrayList<String> btsStatusList = new ArrayList<String>();
		ArrayList<String> btsBookingList = new ArrayList<String>();

		for (int i = 0; i < briefs.length; i++) {
			pageUrlList.add(parsePageUrl(i, briefs[i]));

			name = parseName(i, briefs[i]);
			name = name.replaceAll("[a-zA-Z\\.]+", Constants.STRING_NULL);
			logger.info("\n" + i + "\t" + name);
			content = Constants.STRING_NULL;
			departCity = parseDepartCity(i, briefs[i]);
			routine = parseRoutine(i, briefs[i]);
			departCountry = parseDepartCountry(i, briefs[i]);
			this.currency = Constants.CURRENCY_EUR;
			parseGroup(i, groups[i], pageUrlList.get(i), btsGroupIdList,
					btsDepartDateList, btsEndDateList, btsAdultPriceList,
					btsChildPriceList, btsGroupPriceList, btsStatusList,
					btsBookingList);
			// logger.info(departCountry);
			// update db_lyouu
			CruiseTour.insertCruiseTour(pageUrlList.get(i), Constants.OWNER_DE_PANDA,
					name, Constants.NO_THUMB, content, departCity, routine,
					this.currency, Util.getMinPrice(btsAdultPriceList),
					Constants.PROVIDER_DE_PANDA,
					Util.getMinPrice(btsChildPriceList),
					Util.getMinPrice(btsGroupPriceList), departCountry,
					btsDepartDateList, btsEndDateList, Constants.STRING_NULL,
					Constants.STRING_NULL);
			DaoFactory.getBustourScheduleDao().insert(Constants.OWNER_DE_PANDA,
					name, btsGroupIdList, btsDepartDateList, btsEndDateList,
					this.currency, btsAdultPriceList, btsChildPriceList,
					btsGroupPriceList, btsStatusList, btsBookingList,
					pageUrlList.get(i));
		}
	}

	private String parsePageUrl(int pos, String str) {
		String pageUrl;
		pageUrl = str.split(Constants.SUB_DELIMITER)[0];
		if (pos > 20) {
			if (pageUrl.contains("汉堡+吕贝克2日游"))
				pageUrl = "http://www.pandareisen.de/content2/documents/06-KU-HAM02.pdf";
			else if (pageUrl.contains("汉诺威1日游"))
				pageUrl = "http://www.pandareisen.de/content2/documents/08-KU-HAJ01.pdf";
			else if (pageUrl.contains("法兰克福1日游"))
				pageUrl = "http://www.pandareisen.de/content2/documents/10-KU-FRA01.pdf";
			else if (pageUrl.contains("莱茵河1日游"))
				pageUrl = "http://www.pandareisen.de/content2/documents/02-RHE01.pdf";
			else if (pageUrl.contains("海德堡1日游"))
				pageUrl = "http://www.pandareisen.de/content2/documents/01-HEID01.pdf";
		}
		return pageUrl;
	}

	private String parseName(int pos, String str) {
		String name;
		if (pos < 21)
			name = str.split(Constants.SUB_DELIMITER)[1];
		else
			name = str.split(Constants.SUB_DELIMITER)[0];
		name.replaceAll("[a-zA-Z:\\.//]+", Constants.STRING_NULL);
		return name;
	}

	private String parseRoutine(int pos, String str) {
		String routine;
		if (pos < 21)
			routine = str.split(Constants.SUB_DELIMITER)[3];
		else
			routine = str.split(Constants.SUB_DELIMITER)[2];
		routine = routine.replace("游玩国家", Constants.STRING_NULL);
		routine = routine.replace("游玩城市", Constants.STRING_NULL);
		routine = routine.replace(":",  Constants.STRING_NULL);
		routine = routine.replace("：",  Constants.STRING_NULL);
		routine = routine.replaceAll("、", "-");
		routine = routine.replaceAll("。", Constants.STRING_NULL);
		return routine;
	}

	private String parseDepartCity(int pos, String str) {
		String cities;
		if (pos < 21)
			cities = str.split(Constants.SUB_DELIMITER)[4];
		else
			cities = str.split(Constants.SUB_DELIMITER)[3];
		cities = cities.replaceAll("上下客城市", Constants.STRING_NULL);
		cities = cities.replaceAll("上客城市", Constants.STRING_NULL);
		cities = cities.replaceAll(":",  Constants.STRING_NULL);
		cities = cities.replaceAll("：",  Constants.STRING_NULL);
		cities = cities.replaceAll("、", ", ");
		cities = cities.replaceAll("。", Constants.STRING_NULL);
		logger.info(cities);
		return cities;
	}

	private String parseDepartCountry(int pos, String str) {
		return "64";
	}

	private void parseGroup(int pos, String str, String booking,
			ArrayList<String> btsGroupIdList,
			ArrayList<String> btsDepartDateList,
			ArrayList<String> btsEndDateList,
			ArrayList<String> btsAdultPriceList,
			ArrayList<String> btsChildPriceList,
			ArrayList<String> btsGroupPriceList,
			ArrayList<String> btsStatusList, ArrayList<String> btsBookingList) {
		btsGroupIdList.clear();
		btsDepartDateList.clear();
		btsEndDateList.clear();
		btsAdultPriceList.clear();
		btsChildPriceList.clear();
		btsGroupPriceList.clear();
		btsStatusList.clear();
		btsBookingList.clear();

		String date;
		String price;
		String currencySign;
		if (pos < 21 && pos != 15) {
			String[] groupAry = str.split(Constants.SUB_DELIMITER);
			for (int i = 0; i < groupAry.length; i++) {
				if (!groupAry[i].contains("欢迎报名")) {
					btsGroupIdList.add(Constants.STRING_NULL);
					btsBookingList.add(booking);
					// date
					date = groupAry[i].split(Constants.SUB_SUB_DELIMITER)[0];
					if (date.contains("-")) {
						btsDepartDateList.add(date.split("-")[0]
								.replace(".", Constants.STRING_NULL)
								.replace("月", ".")
								.replace("日", Constants.STRING_NULL));
						btsEndDateList.add(date.split("-")[1]
								.replace(".", Constants.STRING_NULL)
								.replace("月", ".")
								.replace("日", Constants.STRING_NULL));
					}
					// status
					btsStatusList.add(groupAry[i]
							.split(Constants.SUB_SUB_DELIMITER)[1].replace(
							"备注：", Constants.STRING_NULL).trim());
					// price
					price = groupAry[i].split(Constants.SUB_SUB_DELIMITER)[2];
					this.currency = Util.getCurrency(price);
					currencySign = Util.getCurrencySign(price);
					if (currencySign.equals(Constants.CURRENCY_USD_SIGN)) {
						price = price.replaceAll("\\" + currencySign,
								Constants.STRING_NULL);
					} else {
						price = price.replaceAll(currencySign,
								Constants.STRING_NULL);
					}
					btsAdultPriceList.add(price);
					btsChildPriceList.add(Constants.UNKNOWN_PRICE);
					btsGroupPriceList.add(Constants.UNKNOWN_PRICE);
				}
			}
		}
	}

	public static void main(String[] args) {
		CTPandaDe panda = new CTPandaDe();
		try {
			panda.getData();
			panda.printData();
			CruiseTour.deleteSubjects(Constants.OWNER_DE_PANDA);
			panda.updateDbLyouu();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
